package com.lanren.familybookapp.ui.user.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lanren.common.helper.InputTextHelper;
import com.lanren.common.utils.AppUtils;
import com.lanren.common.utils.EditTextUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.widget.ClearEditText;
import com.lanren.common.widget.image.CircleImageView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.ui.main.view.MainActivity;
import com.lanren.familybookapp.ui.user.interfaces.ILoginModel;
import com.lanren.familybookapp.ui.user.presenter.LoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName LoginActivity
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/9/2 15:22
 * @Version 1.0
 */
public class LoginActivity extends BaseUiActivity<LoginPresenter> implements ILoginModel.ILoginView {

    @BindView(R.id.user_name)
    ClearEditText userName;
    @BindView(R.id.password)
    ClearEditText password;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.login_tip)
    TextView loginTip;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.iv_login_logo)
    CircleImageView ivLoginLogo;

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initData();
    }

    @Override
    public void initView() {

    }

    @SuppressLint("SetTextI18n")
    public void initData() {
        if (AppUtils.isDebug()) {
            userName.setText("hengsan");
            password.setText("qwerasdf");
        }
        EditTextUtils.setEditTextFilter(userName);
        new InputTextHelper.Builder(this)
                .setMain(login)
                .addView(userName)
                .addView(password)
                .build();
    }

    @Override
    public void initEvent() {

    }


    @SuppressLint("SetTextI18n")
    @Override
    public void showMessage(String message) {
        showComplete();
        loginTip.setText("提示：" + message);

    }

    @Override
    public void navigateToHome() {
        showComplete();
        loginTip.setVisibility(View.INVISIBLE);
        startActivityFinish(MainActivity.class);
    }

    @OnClick({R.id.login, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                if (Utils.isFastClick(view)) {
                    mPresenter.validateCredentials(userName.getText().toString(), password.getText().toString());
                }
                break;
            case R.id.register:
                if (Utils.isFastClick(view)) {
                    startActivity(RegisterActivity.class);
                }
                break;
        }
    }

}
